﻿using PIMS.Interface;
using PIMS;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using PIMS_Interface.Base;

namespace HBS.InvokeSupport
{
    /// <summary>
    /// https://www.cnblogs.com/pursue/articles/1857407.html
    /// 反射支持类
    /// </summary>
    public class ProxyObject : MarshalByRefObject
    {
        public Assembly assembly = null;

        public void LoadAssembly(string dllPath)
        {
            //assembly = Assembly.LoadFile(dllPath);
            assembly = Assembly.LoadFrom(dllPath);
        }

        public void LoadFile(string dllPath)
        {
            assembly = Assembly.LoadFile(dllPath);
        }

        public object InvokeMethod(string fullClassName, string methodName, params Object[] args)
        {
            object ret = null;
            if (assembly == null)
                return ret;
            Type tp = assembly.GetType(fullClassName);
            if (tp == null)
                return ret;
            MethodInfo method = tp.GetMethod(methodName);
            if (method == null)
                return ret;
            Object obj = Activator.CreateInstance(tp);
            ret=method.Invoke(obj, args);
            return ret;
        }


        public object InvokeMethodShow(string fullClassName, string TabText, params Object[] args)
        {
            object ret = null;
            if (assembly == null)
                return ret;
            Type t = assembly.GetType(fullClassName);
            Object obj = Activator.CreateInstance(t);
            if (!String.IsNullOrEmpty(TabText))
            {
                t.InvokeMember("TabText", BindingFlags.SetProperty, null, obj, new object[] { TabText });
                t.InvokeMember("CloseButton", BindingFlags.SetProperty, null, obj, new object[] { false });
            }
            t.InvokeMember("Show", BindingFlags.InvokeMethod, null, obj, args);
            return ret;
        }

        public object InvokeMethodInitAndShow(string fullClassName, string TabText, IDebugMsg d, ILog4net log, AllPLC allPLC, params Object[] args)
        {
            object ret = null;
            if (assembly == null)
                return ret;
            Type t = assembly.GetType(fullClassName);
            Object obj = Activator.CreateInstance(t);
            if (!String.IsNullOrEmpty(TabText))
            {
                t.InvokeMember("TabText", BindingFlags.SetProperty, null, obj, new object[] { TabText });
                t.InvokeMember("CloseButton", BindingFlags.SetProperty, null, obj, new object[] { false });
            }
            t.InvokeMember("Init", BindingFlags.InvokeMethod, null, obj, new Object[] { d, log, allPLC });
            t.InvokeMember("Show", BindingFlags.InvokeMethod, null, obj, args);
            return ret;
        }

        public object InvokeFormShowDialog(string fullClassName, string propertyName)
        {
            object ret = null;
            Type t = assembly.GetType(fullClassName);
            object obj = Activator.CreateInstance(t);
            t.InvokeMember("ShowDialog", BindingFlags.InvokeMethod, null, obj, new object[] { });
            t.InvokeMember("Close", BindingFlags.InvokeMethod, null, obj, new object[] { });
            if (!String.IsNullOrEmpty(propertyName))
            {
                ret = t.InvokeMember(propertyName, BindingFlags.GetProperty, null, obj, null);
            }
            t.InvokeMember("Dispose", BindingFlags.InvokeMethod, null, obj, new object[] { });
            return ret;
        }

        public object InvokeFormInitAndShowDialog(string fullClassName, string propertyName, IDebugMsg d, ILog4net log, AllPLC allPLC)
        {
            object ret = null;
            Type t = assembly.GetType(fullClassName);
            object obj = Activator.CreateInstance(t);
            t.InvokeMember("Init", BindingFlags.InvokeMethod, null, obj, new Object[] { d, log, allPLC });
            t.InvokeMember("ShowDialog", BindingFlags.InvokeMethod, null, obj, new object[] { });
            t.InvokeMember("Close", BindingFlags.InvokeMethod, null, obj, new object[] { });
            if (!String.IsNullOrEmpty(propertyName))
            {
                ret = t.InvokeMember(propertyName, BindingFlags.GetProperty, null, obj, null);
            }
            t.InvokeMember("Dispose", BindingFlags.InvokeMethod, null, obj, new object[] { });
            return ret;
        }

    }
}
